package com.factors.TangChuan.Physical_Activity;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class PhysicalActivityScoreAvgMapper extends Mapper<LongWritable, Text, Text, DoubleWritable> {

    private static final double[] TARGET_PHYSICAL_ACTIVITIES = {0.0, 2.0, 4.0, 6.0};

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {


        String[] columns = value.toString().split(",");
        if (columns.length >= 20) {
            try {
                double physicalActivity = Double.parseDouble(columns[14]);
                double examScore = Double.parseDouble(columns[19]);


                for (double targetPA : TARGET_PHYSICAL_ACTIVITIES) {
                    if (physicalActivity == targetPA) {

                        context.write(new Text(String.valueOf(targetPA)), new DoubleWritable(examScore));
                        break;
                    }
                }
            } catch (NumberFormatException e) {

            }
        }
    }
}